package com.zhenyi.liuxue.admin;

import com.jfinal.kit.JsonKit;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.liuxue.common.model.LxQuestion;
import com.liuxue.common.model.LxSchool;
import com.liuxue.common.model.LxUkSchool;

public class QuestionController extends AdminController {

	public void list() {
		Long ukSchoolId = getParaToLong("ukSchoolId");
		String keyword = getPara("keyword");
		if (ukSchoolId == null) {
			renderFailure(500, "id is null");
			return;
		}
		LxUkSchool ukSchool = LxUkSchool.dao.findById(ukSchoolId);
		if (ukSchool == null) {
			renderFailure(404, "not found");
			return;
		}
		Kv kv = Kv.create();
		kv.set("unionSchoolId", ukSchool.getUnionSchoolId());
		if (StrKit.notBlank(keyword)) {
			kv.set("keyword", keyword);
			setAttr("keyword", keyword);
		}
		SqlPara sqlPara = Db.use("liuxue").getSqlPara("LXAdminFindQuestion", kv);
		Page<LxQuestion> pages = LxQuestion.dao.paginate(getPage(), getPageSize(), sqlPara);

		setAttr("schoolId", LxSchool.dao.searchFirst("unionSchoolId", ukSchool.getUnionSchoolId()).getId());
		setAttr("ukSchoolId", ukSchoolId);
		setRainbow(pages);
		setItems(JsonKit.toJson(pages.getList()));
		render("/lx/admin/ukSchools/questionList.html");
	}

	public void all() {
		String keyword = getPara("keyword");
		Kv kv = Kv.create();
		if (StrKit.notBlank(keyword)) {
			kv.set("keyword", keyword);
			setAttr("keyword", keyword);
		}
		SqlPara sqlPara = db.getSqlPara("LXAdminFindAllQuestions", kv);
		Page<Record> pages = db.paginate(getPage(), PAGE_SIZE, sqlPara);
		setRainbow(pages);
		setData("items", pages.getList());
		render("/lx/admin/search/index.html");
	}

	public void info() {
		setReferer();
		Long id = getParaToLong("id");
		Long schoolId = getParaToLong("schoolId");
		if (id != null) {
			LxQuestion info = LxQuestion.dao.findById(id);
			if (info == null) {
				renderFailure(404, "not found");
				return;
			}
			setInfoItem(JsonKit.toJson(info));
		}
		setAttr("schoolId", schoolId);
		render("/lx/admin/ukSchools/questionInfo.html");
	}

	public void submit() {
		Long id = getParaToLong("id");
		Long schoolId = getParaToLong("schoolId");
		String question = getPara("question");
		String answer = getPara("answer");

		boolean isNew = id == null;
		LxQuestion info = isNew ? new LxQuestion() : LxQuestion.dao.findById(id);
		if (!isNew && info == null) {
			renderFailure(404, "not found");
			return;
		}
		if (schoolId != null) {
			info.setSchoolId(schoolId);
		}
		info.setQuestion(question);
		info.setAnswer(answer);
		renderFlag(isNew ? info.save() : info.update());
	}

	public void delete() {
		Long id = getParaToLong("id");
		if (id == null) {
			renderFailure(500, "id is null");
			return;
		}
		LxQuestion info = LxQuestion.dao.findById(id);
		if (info == null) {
			renderFailure(404, "not found");
			return;
		}
		renderFlag(info.delete());
	}

}
